import 'package:flutter/material.dart';
import 'dart:async';

class DashboardPage extends StatelessWidget {
  const DashboardPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // 真实已实现功能的模拟统计数据
    final wrongCount = 3; // 错题数
    final prescriptionCount = 28; // 处方总数
    final caseCount = 12; // 医案总数

    return Scaffold(
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 自定义标题栏
              const _CustomTitleBar(),
              const SizedBox(height: 8),
              // 欢迎语
              const Text(
                '欢迎回来，学习者',
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.brown),
              ),
              const SizedBox(height: 8),
              const Text('继续你的六经辨证学习之旅'),
              const SizedBox(height: 24),
              // 中医赞美语动画卡片
              const _PraiseCard(),
              const SizedBox(height: 24),
              // 功能卡片区
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  _FunctionCard(
                    icon: Icons.menu_book,
                    label: '理论学习',
                    onTap: () {},
                  ),
                  _FunctionCard(
                    icon: Icons.assignment,
                    label: '医案管理',
                    onTap: () {},
                    count: caseCount,
                  ),
                  _FunctionCard(
                    icon: Icons.quiz,
                    label: '辨证/处方练习',
                    onTap: () {},
                    count: wrongCount > 0 ? wrongCount : null,
                    countLabel: '错题',
                  ),
                  _FunctionCard(
                    icon: Icons.local_pharmacy,
                    label: '处方查询',
                    onTap: () {},
                    count: prescriptionCount,
                  ),
                ],
              ),
              const SizedBox(height: 24),
              // 真实已实现功能的简要说明
              Card(
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
                color: Colors.brown[50],
                elevation: 0,
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: const [
                      Text('已实现功能', style: TextStyle(fontWeight: FontWeight.bold, color: Colors.brown)),
                      SizedBox(height: 8),
                      Text('• 六经辨证理论学习（分经分类、内容浏览）'),
                      Text('• 医案管理（批量导入、导出、分类、详情）'),
                      Text('• 辨证/处方练习（自定义题库、进度、错题集）'),
                      Text('• 处方查询（多条件检索、详情、导入、对比）'),
                      Text('• 错题复习与进度同步'),
                    ],
                  ),
                ),
              ),
              const SizedBox(height: 24),
              // 版本/数据提示
              const Center(
                child: Text('数据已本地持久化，所有进度与错题均可断点续用', style: TextStyle(color: Colors.brown)),
              ),
              const SizedBox(height: 24),
            ],
          ),
        ),
      ),
    );
  }
}

class _FunctionCard extends StatelessWidget {
  final IconData icon;
  final String label;
  final VoidCallback? onTap;
  final int? count;
  final String? countLabel;
  const _FunctionCard({required this.icon, required this.label, this.onTap, this.count, this.countLabel});

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Card(
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
          elevation: 2,
          child: InkWell(
            borderRadius: BorderRadius.circular(16),
            onTap: onTap,
            child: SizedBox(
              width: 72,
              height: 72,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(icon, size: 32, color: Colors.brown),
                  const SizedBox(height: 4),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 2),
                    child: Text(
                      label,
                      style: const TextStyle(fontSize: 12),
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      textAlign: TextAlign.center,
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
        if (count != null && count! > 0)
          Positioned(
            right: 4,
            top: 4,
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
              decoration: BoxDecoration(
                color: Colors.redAccent,
                borderRadius: BorderRadius.circular(12),
              ),
              child: Text(
                countLabel != null ? '$countLabel$count' : '$count',
                style: const TextStyle(color: Colors.white, fontSize: 11, fontWeight: FontWeight.bold),
              ),
            ),
          ),
      ],
    );
  }
}

// 动画中医赞美语卡片
class _PraiseCard extends StatefulWidget {
  const _PraiseCard({Key? key}) : super(key: key);

  @override
  State<_PraiseCard> createState() => _PraiseCardState();
}

class _PraiseCardState extends State<_PraiseCard> with SingleTickerProviderStateMixin {
  final List<String> _praises = [
    '岐黄薪火，传承千载。',
    '经方之道，济世安人。',
    '六经辨证，融汇古今智慧。',
    '医道精微，仁心济世。',
    '中医药，中华瑰宝。',
  ];
  int _index = 0;
  late Timer _timer;
  late AnimationController _controller;
  late Animation<double> _fade;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this, duration: const Duration(milliseconds: 800));
    _fade = CurvedAnimation(parent: _controller, curve: Curves.easeInOut);
    _controller.forward();
    _timer = Timer.periodic(const Duration(seconds: 3), (timer) {
      _controller.reverse().then((_) {
        setState(() {
          _index = (_index + 1) % _praises.length;
        });
        _controller.forward();
      });
    });
  }

  @override
  void dispose() {
    _timer.cancel();
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
      color: Colors.brown[100]?.withOpacity(0.2),
      elevation: 1,
      child: Padding(
        padding: const EdgeInsets.all(24.0),
        child: Row(
          children: [
            const Icon(Icons.auto_awesome, color: Colors.brown, size: 40),
            const SizedBox(width: 16),
            Flexible(
              child: FadeTransition(
                opacity: _fade,
                child: Text(
                  _praises[_index],
                  style: const TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                    color: Colors.brown,
                    letterSpacing: 2,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

// 自定义标题栏组件
class _CustomTitleBar extends StatelessWidget {
  const _CustomTitleBar({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 18, horizontal: 0),
      decoration: BoxDecoration(
        color: Colors.brown[50],
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: Colors.brown.withOpacity(0.04),
            blurRadius: 4,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: const [
          Icon(Icons.local_florist, color: Colors.brown, size: 28),
          SizedBox(width: 8),
          Text(
            '胡希恕六经辨证学习系统',
            style: TextStyle(
              fontSize: 20,
              fontWeight: FontWeight.bold,
              color: Colors.brown,
              letterSpacing: 2,
            ),
          ),
        ],
      ),
    );
  }
} 